package com.solution.admin.point.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.point.entity.PointLogEntity;
import com.solution.admin.point.service.PointLogService;
import com.solution.common.constants.PublicStatus.SysCode;
import com.solution.common.controller.BaseController;
import com.solution.common.entity.BaseReturn;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 积分日志信息web控制层
 * @author xu_cc
 * @date 2017-10-20 14:18:04
 */
@RequestMapping("/pointLog")
@Controller
public class PointLogController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(PointLogController.class);

    @Autowired
    private PointLogService pointLogService;
    
    /**
     * 跳转积分日志信息查询页面
     * @param PointLog 接收界面查询条件参数
     * @param request 请求对象
     * @return 返回对应的页面视图
     * @author xu_cc
      * @date 2017-10-20 14:18:04
     */
    @RequestMapping("/toListPointLogPage")
    public String toListPointLogPage(PointLogEntity pointLog, HttpServletRequest request) {
        return "point/pointLog_list";
    }

    /**
     * 分页查询积分日志信息列表
     * @param pointLog 接收界面查询条件参数
     * @param request 请求对象
     * @return 返回对应的页面视图
     * @author xu_cc
      * @date 2017-10-20 14:18:04
     */
    @RequestMapping("/listPointLogPage")
    @ResponseBody
    public JSONGrid listPointLogPage(PointLogEntity pointLog, HttpServletRequest request){
        int pageNum=Integer.parseInt(request.getParameter("page").toString());
        int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
        PageParam pageParam = super.getPageParam(request);
        pageParam.setPageNum(pageNum);
        pageParam.setNumPerPage(numPerPage);
        Map<String, Object> paramMap = new HashMap<String, Object>();
        try {
            PageBean pageBean = pointLogService.listPointLogPage(pageParam, paramMap);
            return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
        } catch (Exception e) {
            handleException("分页查询积分日志信息失败", logger, e);
        }
        return new JSONGrid();
    }
    
    
    /**
     * 跳转新增页面
     * @return string 跳转页面路径
     * @author xu_cc
      * @date 2017-10-20 14:18:04
     */
    @RequestMapping("/toAddPointLog")
    public String toAddPointLog(HttpServletRequest request) {
        request.setAttribute("opFlag", "add");
        return "point/pointLog_form";
    }

    /**
     * 跳转编辑页面
     * @return string 跳转页面路径
     * @author xu_cc
      * @date 2017-10-20 14:18:04
     */
    @RequestMapping("/toUpdatePointLog")
    public String toUpdatePointLog(HttpServletRequest request) {
        // 编辑操作
        request.setAttribute("opFlag", "edit");
        long id = Long.valueOf(request.getParameter("id"));
        try {
            PointLogEntity pointLog = pointLogService.getPointLogById(id);
            request.setAttribute("pointLog", pointLog);
        } catch (Exception e) {
            handleException("跳转编辑积分日志信息页面错误", logger, e);
        }
        return "point/pointLog_form";
    }


    /**
     * 新增或保存积分日志信息
     * @param request 请求对象
     * @param pointLogEntity 积分日志信息实体
     * @return
     * @author xu_cc
      * @date 2017-10-20 14:18:04
     */
    @RequestMapping("/saveOrUpdatePointLog")
    @ResponseBody
    public BaseReturn savePointLog(HttpServletRequest request, PointLogEntity pointLogEntity) {
        BaseReturn result = new BaseReturn();
        try {
            String id = request.getParameter("id");
            if(StringUtil.isNotEmpty(id)){
                pointLogService.updatePointLog(pointLogEntity);
            }else{
                pointLogService.savePointLog(pointLogEntity);
            }
            result.setCode(SysCode.SUCCESS);
            result.setMsg("积分日志信息保存成功！");
        } catch (Exception e) {
            result.setCode(SysCode.ERROR);
            result.setMsg("积分日志信息保存异常！");
        }
        return result;
    }
    
}
